package controller.game.player;

import entity.CounterMessage;
import entity.Message;
import entity.NegoStatue;
import entity.ServerMessage;
import entity.domain.value.Value;
import entity.evaluation.EvaluatorHelper;
import entity.offer.AcceptOffer;
import entity.offer.CounterOffer;
import entity.offer.Offer;
import entity.offer.Type;
import entity.offer.bid.Bid;
import ui.MessageFrame;
import ui.message.GamePanel;
import util.Time;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Human extends Player{

    private GamePanel panel;
    private Object sendButtonLock;

    public Human(){}

    @Override
    public void initPlayer(int roomNum, int playerNum, EvaluatorHelper helper){
        super.initPlayer(roomNum, playerNum, helper);
        this.sendButtonLock = new Object();
        GamePanel panel = GamePanel.createMessagePanel(helper.getDomain().getName(), helper, this.sendButtonLock);
        panel.displayServerMessage(Time.getHms(),"您是第"+(playerNum+1)+"个玩家。");
        this.panel = panel;
    }

    @Override
    public void receiveMessage(Message message) {
        if(message instanceof CounterMessage){
            CounterMessage counterMessage = ((CounterMessage) message);
            List<Integer> speakers = counterMessage.getSpeakers();
            if(speakers.contains(playerNum)){
                panel.setButtonEnabled(true);
            } else {
                panel.setButtonEnabled(false);
            }
            String sender = String.valueOf(message.getSource());
            String time = counterMessage.getTime();
            Offer offer = counterMessage.getBody();
            double evaluation = helper.getNormalizedEvaluation(offer.getBid());
            panel.displayCounterMessage(sender, time, offer.toString(), evaluation, counterMessage.getComment());
        }else if(message instanceof ServerMessage){
            ServerMessage msg = (ServerMessage) message;
            switch (msg.getEvent()){
                case Client_Disconnect:
                    String content = msg.getBody();
                    panel.displayServerMessage(msg.getTime(), content);
                    panel.setButtonEnabled(false);
                    break;
                case Client_Connect:
                    //frame.receiveServMsg(msg.getBody());
                    panel.displayServerMessage(msg.getTime(), msg.getBody());
                    /*if(msg.getInfo().getStatue()== NegoStatue.OnNego&&this.playerNum==msg.getInfo().getLastSpeakerNum()){
                        //frame.setSenderButtonState(true);
                        unlockSender();
                        panel.setSendButtonEnabled(true);
                    }*/
                    break;
                case Nego_Finished:
                    panel.displayServerMessage(msg.getTime(), msg.getBody());
                    panel.setButtonEnabled(false);
                    break;
                case Nego_Start:
                    panel.displayServerMessage(msg.getTime(), msg.getBody());
                    if(this.playerNum == 0){
                        panel.setSendButtonEnabled(true);
                    }
                    break;
            }
        }
    }

    @Override
    public CounterMessage sendMessage() {
        synchronized (sendButtonLock){
            try {
                sendButtonLock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        CounterMessage message = new CounterMessage(this.playerNum, this.roomNum);
        if(panel.getIsAccept()){
            AcceptOffer acceptOffer = new AcceptOffer(lastCounterMessage.getBody().getBid());
            message.setBody(acceptOffer);
        }else{
            List<Value> values = Arrays.asList(panel.getValues());
            CounterOffer counterOffer = new CounterOffer(new Bid(values));
            message.setBody(counterOffer);
        }
        String comment = panel.getCommentInput();
        message.setComment(comment);
        return message;
    }

}
